﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

public partial class regist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void txtName_TextChanged1(object sender, EventArgs e)
    {

    }

    protected void txtPhone_TextChanged1(object sender, EventArgs e)
    {

    }

    protected void txtPassword_TextChanged1(object sender, EventArgs e)
    {

    }

    protected void txtNick_TextChanged(object sender, EventArgs e)
    {

    }

    protected void btnRegist_Click1(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string str = ("Server=(local);Integrated Security=SSPI;database=Pets");
            SqlConnection connect = new SqlConnection(str);
            connect.Open(); //打开连接
            //检测用户名是否存在
            {
                string Phone = this.txtPhone.Text;
                //创建SQL语句
                string selStr = "select * from [User] where Phone='" + Phone + "'";
                //创建数据适配器
                SqlDataAdapter da = new SqlDataAdapter(selStr, connect);
                //创建满足条件的数据集
                DataSet ds = new DataSet();
                da.Fill(ds);
                //如果数据集不为空，则用户名已经存在
                if (ds.Tables[0].Rows.Count != 0)
                {
                    MessageBox.Show("用户名已经存在！");
                    Response.AddHeader("Refresh", "0");
                }
                else
                {
                    string strsql = "insert into [User] (UserName,Phone,Password,Nick) values ('" + txtName.Text + "','" + txtPhone.Text + "','" + txtPassword.Text + "','" + txtNick.Text + "')";
                    SqlCommand cmd = new SqlCommand(strsql, connect); //创建执行
                    cmd.ExecuteNonQuery(); //执行SQL

                    MessageBox.Show("注册成功，返回登录！");
                    Response.Redirect("Login.aspx", true);
                }
            }

        }
    }

    protected void lnkLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx", true);
    }
}
